package org.statewalker.parser.wiki.tokenizer;

import org.statewalker.tokenizer.CharStream;
import org.statewalker.tokenizer.CompositeTokenizer;
import org.statewalker.tokenizer.StreamToken;
import org.statewalker.tokenizer.CharStream.Marker;
import org.statewalker.tokenizer.CharStream.Pointer;
import org.statewalker.tokenizer.base.NewLineTokenizer;

public class BlockTokenizer extends CompositeTokenizer {

    public BlockTokenizer() {
    }

    @Override
    public StreamToken read(CharStream stream) {
        Marker marker = stream.markPosition();
        StreamToken result = null;
        try {
            Pointer ptr = marker.getPointer();
            if (ptr.column == 0
                || NewLineTokenizer.readNewLine(stream) != null
                || stream.isNewContext()) {
                result = super.read(stream);
            }
            return result;
        } finally {
            marker.close(result == null);
        }
    }
}
